<?php

namespace App\Modules\Health\Http\Controllers\Admin;

use App\Modules\Health\Repositories\Body;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class BodyController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Body(), function (Grid $grid) {
            $grid->model()->orderByDesc('id');

            $grid->column('id');
            $grid->column('name');
            $grid->column('img')->image(' ', 50, 50);
            $grid->column('status')->using(['禁用', '正常'])->label(['danger', 'primary']);
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->equal('id')->width(2);
                $filter->like('name')->width(3);
                $filter->between('created_at')->datetime()->width(4);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Body(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('img');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Body(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->image('img')->required()->autoUpload()->saveFullUrl()->uniqueName();
            $form->radio('status')->options(['禁用', '正常'])->default(1);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
